package org.dreamwork.peony;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-1-21
 * Time: 11:43:47
 */
public class PropertyDescriptor {
    private String property;
    private Class<?> claxx;

    public PropertyDescriptor (String property, Class<?> claxx) {
        this.property = property;
        this.claxx = claxx;
    }

    public Class getPropertyType () throws NoSuchMethodException {
        Method reader = getReader ();
        return reader.getReturnType ();
    }

    public Method getReader (Class<?>... params) throws NoSuchMethodException {
        String reader = "get" + Character.toUpperCase (property.charAt (0)) + property.substring (1);
        return claxx.getMethod (reader, params);
    }

    public Method getWriter (Class<?>... params) throws NoSuchMethodException {
        String setter = "set" + Character.toUpperCase (property.charAt (0)) + property.substring (1);
        return claxx.getMethod (setter, params);
    }

    public void setValue (Object obj, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method setter = getWriter ();
        setter.invoke (obj, value);
    }

    public Object getValue (Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method getter = getReader ();
        return getter.invoke (obj);
    }
}